package main

import (
	"github.com/streadway/amqp"
	"gitee.com/hlwqds/rabitmqTest/log"
	"os"
	"strings"
)

func bodyFrom(args []string) string{
	var s string
	if len(args) < 2 || args[1] == "" {
		s = "hello"
	}else{
		s = strings.Join(args[1:], " ")
	}
	return s
}

func main(){
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	log.FailOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	//创建channel
	ch, err := conn.Channel()
	log.FailOnError(err, "Failed to open a channel")
	defer ch.Close()

	//声明消息队列
	q, err := ch.QueueDeclare(
		//队列持久化
		"helloDurable",
		true,
		false,
		false,
		false,
		nil)
	log.FailOnError(err, "Failed to declare a queue")

	body := bodyFrom(os.Args)
	err = ch.Publish(
		"",
		q.Name,
		false,
		false,
		amqp.Publishing{
			//消息持久化
			DeliveryMode: amqp.Persistent,
			ContentType: "text/plain",
			Body: []byte(body),
		})
	log.FailOnError(err, "Failed to publish a message")
}